package com.army.study;

import com.army.study.database.DatabaseUtils;
import com.army.study.database.FormDatabase;
import com.army.study.database.Form;
import com.army.study.database.Sensor;
import com.army.study.utils.ChartDataUtils;
import com.army.study.utils.DateUtils;
import com.army.study.utils.LogUtils;
import ohos.aafwk.ability.*;
import ohos.aafwk.content.Intent;
import ohos.data.DatabaseHelper;
import ohos.data.orm.OrmContext;
import ohos.utils.zson.ZSONObject;

public class StepFormAbility extends Ability {
    private static final String TAG = StepFormAbility.class.getSimpleName();
    private static final String EMPTY_STRING = "";
    private static final int INVALID_FORM_ID = -1;
    private static final int DEFAULT_DIMENSION_2X2 = 2;
    private DatabaseHelper helper = new DatabaseHelper(this);
    private OrmContext connect;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        connect = helper.getOrmContext("FormDatabase", "FormDatabase.db", FormDatabase.class);
    }

    @Override
    protected ProviderFormInfo onCreateForm(Intent intent) {
        ProviderFormInfo providerFormInfo = new ProviderFormInfo();
        if (intent == null) {
            return providerFormInfo;
        }

        // 获取卡片id
        long formId = INVALID_FORM_ID;
        if (intent.hasParameter(AbilitySlice.PARAM_FORM_IDENTITY_KEY)) {
            formId = intent.getLongParam(AbilitySlice.PARAM_FORM_IDENTITY_KEY, INVALID_FORM_ID);
        }

        // 获取卡片名称
        String formName = EMPTY_STRING;
        if (intent.hasParameter(AbilitySlice.PARAM_FORM_NAME_KEY)) {
            formName = intent.getStringParam(AbilitySlice.PARAM_FORM_NAME_KEY);
        }

        // 获取卡片规格
        int dimension = DEFAULT_DIMENSION_2X2;
        if (intent.hasParameter(AbilitySlice.PARAM_FORM_DIMENSION_KEY)) {
            dimension = intent.getIntParam(AbilitySlice.PARAM_FORM_DIMENSION_KEY, DEFAULT_DIMENSION_2X2);
        }

        connect = helper.getOrmContext("FormDatabase", "FormDatabase.db", FormDatabase.class);

        // 存储卡片信息
        Form form = new Form(formId, formName, dimension);
        DatabaseUtils.insertForm(form, connect);

        // 获取当天的步数
        Sensor sensor = DatabaseUtils.getSensorData(connect, DateUtils.getDate(0));
        String stepValue = "";
        if (sensor != null) {
            stepValue = sensor.getStepsValue() + "";
        } else {
            stepValue = "0";
        }

        // 获取卡片页面需要的数据
        ZSONObject zsonObject = ChartDataUtils.getZSONObject(stepValue, dimension, connect);
        providerFormInfo.setJsBindingData(new FormBindingData(zsonObject));

        return providerFormInfo;
    }

    @Override
    protected void onDeleteForm(long formId) {
        LogUtils.info(TAG, "onDeleteForm():formId=" + formId);
        super.onDeleteForm(formId);
        // 删除数据库中的卡片信息
        DatabaseUtils.deleteFormData(formId, connect);
    }

    @Override
    protected void onUpdateForm(long formId) {
        LogUtils.info(TAG, "onUpdateForm()");
        super.onUpdateForm(formId);
        ZSONObject zsonObject = new ZSONObject();
        zsonObject.put(EMPTY_STRING, EMPTY_STRING);
        try {
            updateForm(formId, new FormBindingData(zsonObject));
        } catch (FormException e) {
            LogUtils.error(TAG, e.getMessage());
        }
    }
}
